package com.cqc.volleydemo1.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.cqc.volleydemo1.R;
import com.cqc.volleydemo1.application.MyApplication;
import com.cqc.volleydemo1.net.MainRequest;
import com.cqc.volleydemo1.net.NetUrl;
import com.cqc.volleydemo1.net.RequestTag;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //post代参请求
//        requestPostNews();

        //get代参请求,失败，getHeads()
        requestGetJoke();

        //get请求，参数采用拼接的方式。
//        requestGetJoke2();
    }

    private void requestPostNews() {
        Map<String, String> map = new HashMap<>();
        map.put("key", "593143c7f820e7be85d82b72fa71ea5f");
        //post带参数请求
        new MainRequest().makePostRequest(NetUrl.NEWSTOP, map, RequestTag.MAINACTIVITY);
    }

    private void requestGetJoke() {
        Map<String, String> map = new HashMap<>();
        map.put("key", "f1b5d3fd04ac2fa350e8f9fef50af6e7");
        map.put("sort", "desc");
        map.put("time", "1418816972");
        new MainRequest().makeGetRequest(NetUrl.JOKE, map, RequestTag.MAINACTIVITY);
    }

    private void requestGetJoke2() {
        //http://japi.juhe.cn/joke/content/list.from?key=您申请的KEY&page=2&pagesize=10&sort=asc&time=1418745237
//        String url = NetUrl.JOKE + "?"
//                +"key=f1b5d3fd04ac2fa350e8f9fef50af6e7" +"&"
//                +"page=2" +"&"
//                +"agesize=10" +"&"
//                +"sort=asc" +"&"
//                +"time=1418745237";
        //按照聚合API，只需要传key + sort + time
        String url = NetUrl.JOKE + "?"
                + "key=f1b5d3fd04ac2fa350e8f9fef50af6e7" + "&"
                + "sort=asc" + "&"
                + "time=1418745237";
        new MainRequest().makeGetRequest(url, RequestTag.MAINACTIVITY);
    }
}
